import { createRouter, createWebHashHistory } from "vue-router";
import { getCookie } from "@/Hook/cookie";
import { showToast } from "vant";
const routes = [
  { path: "/", redirect: "/home" },
  {
    path: "/home",
    component: () => import("@/views/home"),
    meta: {
      isShowFooter: true,
    },
  },
  //目的地的
  {
    path: "/place",
    component: () => import("@/views/place"),
    meta: {
      isShowFooter: true,
    },
  },
  //马儿定制
  {
    path: "/customization",
    component: () => import("@/views/customization"),
    meta: {
      isShowFooter: true,
    },
  },
  //定制师详情
  {
    path: "/DzsDetail",
    component: () => import("@/views/customization/DzsDetail.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //发现
  {
    path: "/discover",
    component: () => import("@/views/discover"),
    meta: {
      isShowFooter: true,
    },
  },
  //发现详情
  {
    path: "/findDetail/:id",
    component: () => import("@/views/discover/findDetail.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //我的
  {
    path: "/mine",
    component: () => import("@/views/mine"),
    meta: {
      isShowFooter: true,
    },
    beforeEnter: (to, from, next) => {
      const uid = getCookie("uid");
      console.log(uid);
      console.log(from);
      if (uid) {
        next();
      } else {
        showToast(`您还未登录！请先登录`);
        next("/login");
      }
      // return false;
    },
  },
  //我的订单
  {
    path: "/MayOrder",
    component: () => import("@/views/mine/MayOrder.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //用户反馈
  {
    path: "/Feedback",
    component: () => import("@/views/mine/Feedback.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //设置
  {
    path: "/Set",
    component: () => import("@/views/mine/Set.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //用户协议
  {
    path: "/Agreement",
    component: () => import("@/views/mine/Agreement.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //特别声明
  {
    path: "/Statement",
    component: () => import("@/views/mine/Statement.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //关于我们
  {
    path: "/AboutUs",
    component: () => import("@/views/mine/AboutUs.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //登录注册
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    meta: {
      isShowFooter: false,
    },
  },
  //密码登录
  {
    path: "/loginpass",
    component: () => import("@/views/login/loginpass.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //验证码登录
  {
    path: "/verification",
    component: () => import("@/views/login/verification.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //验证码登录
  {
    path: "/agreement",
    component: () => import("@/views/login/agreement.vue"),
    meta: {
      isShowFooter: false,
    },
  },
  //城市酒店列表
  {
    path: "/citys",
    component: () => import("@/views/citys"),
    meta: {
      isShowFooter: false,
    },
  },
  //详情
  {
    path: "/detail",
    component: () => import("@/views/detail"),
    meta: {
      isShowFooter: false,
    },
  },
  //选着团期
  {
    path: "/selectDate",
    component: () => import("@/views/selectDate"),
    meta: {
      isShowFooter: false,
    },
  },
  //选着团期提交预期
  {
    path: "/trip",
    component: () => import("@/views/trip"),
    meta: {
      isShowFooter: false,
    },
  },
  //搜索
  {
    path: "/search",
    component: () => import("@/views/search"),
    meta: {
      isShowFooter: false,
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
});
// router.beforeEach((to, from, next) => {
//   // to from  $route
//   // console.log(from, to);
//   // next(); //放行
//   if (to.meta.login) {
//     // 需要登录才能访问的
//     if (localStorage.getItem("uid")) {
//       // 有uid 说明是登录状态
//       next();
//     } else {
//       // 拦截至登录页
//       Toast("您还未登录!\n请先登录\n2秒");
//       let second = 2;
//       const timer = setInterval(() => {
//         second--;
//         if (second) {
//           Toast(`您还未登录!\n请先登录\n  ${second} 秒`);
//         } else {
//           clearInterval(timer);
//           Toast.clear();
//           next("/login?from=" + encodeURIComponent(to.fullPath));
//         }
//       }, 1000);
//     }
//   } else {
//     // 不需要登录验证的
//     next();
//   }
// });
// router.beforeEach((to, from, next) => {
//   let uid = getToken("uid");
//   // let uid = setCookie("uid");
//   // console.log(uid);
//   console.log(to, from);
//   console.log(uid);
//   if (!uid) {
//     //判断有没有token，当前用户没有登录要到登录页
//     if (to.path == "/login") {
//       next();
//     } else {
//       next(to.path == "/login");
//     }
//   } else {
//     //用户已经登陆
//     if (to.path == "/login") {
//       //用户已经登录了，正在登录页，打回首页
//       next("/");
//     } else {
//       next();
//     }
//   }
// });

export default router;
